package com.qiqidream.admin.system.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiqidream.admin.common.base.BaseController;
import com.qiqidream.admin.common.constant.StaticConstants;
import com.qiqidream.admin.common.enums.MenuType;
import com.qiqidream.admin.common.utils.tree.TreeUtil;
import com.qiqidream.admin.system.model.dto.MenuDto;
import com.qiqidream.admin.system.model.entity.Menu;
import com.qiqidream.admin.system.model.vo.MenuTreeOptionVo;
import com.qiqidream.admin.system.model.vo.MenuVo;
import com.qiqidream.admin.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author QiQiDream
 * @since 2019/12/4 17:41
 */
@RestController
@Api(tags = "系统：菜单管理接口")
@RequestMapping("/system/menu")
public class MenuController extends BaseController {

    @Resource
    MenuService menuService;

    @ApiOperation(value = "查询所有菜单")
    @GetMapping("")
    public List<MenuVo> getMenus() {
        return menuService.getMenus();
    }

    @ApiOperation(value = "添加菜单")
    @PostMapping("")
    public Boolean addMenu(@RequestBody MenuDto menuDto) {
        return menuService.addMenu(menuDto);
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/{id}")
    public Boolean removeMenu(@PathVariable Long id) {
        return menuService.removeMenu(id);
    }

    @ApiOperation(value = "编辑菜单")
    @PutMapping("")
    public Boolean editMenu(@RequestBody MenuDto menuDto) {
        return menuService.editMenu(menuDto);
    }

    @ApiOperation(value = "菜单-树选择器Option")
    @GetMapping("/option/tree")
    public List<MenuTreeOptionVo> getMenuTreeSelect(@RequestParam(value = "hasButton",required = false) Boolean hasButton) {
        List<Menu> menus;
        if (!ObjectUtils.isEmpty(hasButton) && hasButton){
            menus = menuService.list(new QueryWrapper<Menu>().orderByAsc("type","sort"));
        } else {
            menus = menuService.list(new LambdaQueryWrapper<Menu>().ne(Menu::getType, MenuType.BUTTON).orderByAsc(Menu::getType).orderByAsc(Menu::getSort));
        }
        List<MenuTreeOptionVo> convert = Convert.convert(new TypeReference<List<MenuTreeOptionVo>>() {}, menus);
        return TreeUtil.getTreeList(0L, convert);
    }
}
